package com.extension.company.view.my.address;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.extension.company.admin.R;
import com.extension.company.common.mvp.BaseTitleActivity;
import com.extension.company.common.utils.ToastUtil;
import com.extension.company.common.widget.dialog.DialogOK;
import com.extension.company.contract.AddressListContract;
import com.extension.company.model.entity.AddressListData;
import com.extension.company.presenter.my.AddressListPresenter;
import com.extension.company.view.adapter.AddressListAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import butterknife.BindView;

/**
 * 收货地址 -展示列表
 */
public class AddressListActivity extends BaseTitleActivity<AddressListContract.View, AddressListPresenter>
		implements AddressListContract.View, SwipeRefreshLayout.OnRefreshListener, AddressListAdapter.DelOnclickListener {
	@BindView(R.id.no_data)
	LinearLayout noData;
	@BindView(R.id.have_data)
	LinearLayout haveData;
	@BindView(R.id.address_smrefresh)
	SmartRefreshLayout smartRefreshLayout;
	@BindView(R.id.address_recycle)
	RecyclerView recyclerView;

	private List<AddressListData.DataBean.AddressBean> addressList;
	private AddressListAdapter adapter;
	private int delePosition;
	private boolean waitForPaymentActivityJump; // true 待支付的，false
	public static final int ADDRESS_RESULT = 0x122;
	private AddressListData.DataBean.AddressBean newAddress; // 默认地址取消后数据信息
	private int defaultId; // 更改选中的默认地址的id


	public static void startActivity(Activity activity) {
		Intent intent = new Intent(activity, AddressListActivity.class);
		activity.startActivity(intent);
	}


	@Override
	protected void onResume() {
		super.onResume();
		presenter.requestAddressList(); // 请求地址列表
	}


	@Override
	protected void initData() {
		initView();
		waitForPaymentActivityJump = getIntent().getBooleanExtra("OnlineWaitForPayActivity", false); // 是否是待支付界面跳转的
	}

	/**
	 * 初始化view
	 */
	private void initView() {
		setTitle("收货地址");
		setShowRight(true);
		setTvRight("新增地址");
		addressList = new ArrayList<>();
		smartRefreshLayout.autoRefresh();
		adapter = new AddressListAdapter(this, addressList);
		recyclerView.setAdapter(adapter);

	}

	@Override
	protected void initListener() {
		adapter.setDelClickListener(this);
	}

	@Override
	public int getMainLayout() {
		return R.layout.addresslist;
	}

	@Override
	public AddressListContract.View createView() {
		return this;
	}

	@Override
	public AddressListPresenter createPresenter() {
		return new AddressListPresenter();
	}

	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}

	@Override
	public void action_Right(View v) {   // 新增收货地址按钮
		super.action_Right(v);
		AddAddressActivity.startAddressListAdd(this);
	}

	@Override
	public void dataCallBackSuccess(List<AddressListData.DataBean.AddressBean> list) { // 数据请求回调成功
		if (null != list && 0 == list.size()) {
			noData.setVisibility(View.VISIBLE);
			haveData.setVisibility(View.GONE);
		} else if (null != list) {
			noData.setVisibility(View.GONE);
			haveData.setVisibility(View.VISIBLE);

			addressList.clear();
			addressList.addAll(list);
			adapter.setContent(addressList);

			if (smartRefreshLayout.getState() == RefreshState.Refreshing) {  // 关闭刷新
				smartRefreshLayout.finishRefresh(true);
			}
		}

	}

	@Override
	public void requestFaile(String msg) {
//		ToastUtil.showToast(this, msg);
//		mRecyclerView.setPullRefreshCompleted();
	}

	@Override
	public void onRefresh() {   // 刷新
		if (null != addressList && addressList.size() != 0) {
			addressList.clear();
		}
		presenter.requestAddressList(); // 重新请求数据
	}


	@Override
	public void delItem(int delDataId, int position) { // item中点击删除的
		this.delePosition = position;
		presenter.deleteAddress(String.valueOf(delDataId));

	}

	@Override
	public void deleteSuccess(String msg) {  // 数据删除成功
		ToastUtil.showToast(this, msg);
		addressList.remove(delePosition);
//		adapter.setContent(addressList);
		adapter.notifyDataSetChanged();
	}

	@Override
	public void defaultAddress(boolean isSetSuccess) { // 默认选中的地址
		if (isSetSuccess) {
			// 请求网络刷新地址的列表
			presenter.requestAddressList();
		}

	}

	@Override
	public void editItem(AddressListData.DataBean.AddressBean addressBean) { // 点击编辑
		Intent intent = new Intent(this, AddAddressActivity.class);
		intent.putExtra("addressInfo", addressBean);
		startActivity(intent);
	}

	@Override
	public void click(AddressListData.DataBean.AddressBean addressBean) { // 点击item
		if (waitForPaymentActivityJump) {
			Intent intent = new Intent();
			intent.putExtra("orderAddress", addressBean);
			setResult(ADDRESS_RESULT, intent);
			finish();
		}

	}

	/**
	 * 在无默认的地址的情况下，选中默认地址的监听事件
	 *
	 * @param list                   数据列表
	 * @param defaultAddressPosition item中checkbox选中的item的位置
	 * @param isDefaultAddress       默认地址 1  其他是0   true  是设置为默认你地if址，false 是取消默认地址
	 */
	@Override
	public void defaultAddressCheck(final List<AddressListData.DataBean.AddressBean> list, final int defaultAddressPosition, boolean isDefaultAddress) {
		if ("1".equals(list.get(defaultAddressPosition).getIs_default())) { // 如果点击的item原本就是默认地址
			// 取消默认地址，更新地址信息，
			list.get(defaultAddressPosition).setIs_default("0");
			AddressListData.DataBean.AddressBean newAddress = list.get(defaultAddressPosition);

		} else { // 如果原来不是默认地址，设置默认地址，只需获取其id就可。弹出个dialog，询问下是否保存，提交到无服务器
			defaultId = list.get(defaultAddressPosition).getId();
			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
			DialogOK dialogOK = new DialogOK(this);
			dialogOK.setTitle("默认收货地址");
			dialogOK.setContent("保存更改");
			dialogOK.setButtonName("取消", "保存");
			dialogOK.setDialogOKListener(new DialogOK.DialogOKListener() {
				@Override
				public void dialogOKListenerOK() {
					// 网络请求设置默认的收货地址
					presenter.defaultAddress(list.get(defaultAddressPosition).getId());
				}

				@Override
				public void dialogOKListenerCancel() {
					// 取消保存

				}
			});
			dialogOK.show();

		}
	}

}
